using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

using libsecondlife;
using Feathertail;
using libsecondlife.Packets;


namespace NestBox
{
    class Balance : Command
    {
        protected SecondLife _Client;
        protected LLUUID BalanceRequestID = LLUUID.Zero;
        protected bool MonitorBalanceReplies = false;
        protected Mutex BalanceRequestMutex = new Mutex();

        #region Command Members

        public void Setup(FeathertailClient Client)
        {
            _Client = Client;
            _Client.Self.OnMoneyBalanceReplyReceived += new MainAvatar.MoneyBalanceReplyCallback(Self_OnMoneyBalanceReplyReceived);
        }

        void Self_OnMoneyBalanceReplyReceived(LLUUID transactionID, bool transactionSuccess, int balance, int metersCredit, int metersCommitted, string description)
        {
            BalanceRequestMutex.WaitOne();

            if( (BalanceRequestID != transactionID) && (MonitorBalanceReplies == false) )
            {
                // We didn't request this, and we're not monitoring
                BalanceRequestMutex.ReleaseMutex();
                return;
            } else if (BalanceRequestID == transactionID) {
                // We requested this, so go ahead and reset the request id
                BalanceRequestID = LLUUID.Zero;
            }
            BalanceRequestMutex.ReleaseMutex();

            Console.WriteLine();
            Console.WriteLine("Balance: L$" + balance);
            Console.WriteLine("Transaction Success: " + transactionSuccess);
            Console.WriteLine("Meters Credit: " + metersCredit);
            Console.WriteLine("Meters Committed: " + metersCommitted);
            Console.WriteLine("Description: " + description);

                    
        }

        public string HandleInput(string[] CmdLine)
        {
            if ((CmdLine.Length == 2) && (CmdLine[1].ToLower().Equals("monitor")))
            {
                if (MonitorBalanceReplies)
                {
                    MonitorBalanceReplies = false;
                    return "No longer monitoring balance replies.";
                }
                else
                {
                    MonitorBalanceReplies = true;
                    return "Now monitoring all balance replies.";
                }
            }

            BalanceRequestMutex.WaitOne();

            string rtn;
            if (BalanceRequestID == LLUUID.Zero)
            {
                BalanceRequestID = LLUUID.Random();

                MoneyBalanceRequestPacket mbrp = new MoneyBalanceRequestPacket();
                mbrp.AgentData.AgentID = _Client.Network.AgentID;
                mbrp.AgentData.SessionID = _Client.Network.SessionID;
                mbrp.MoneyData.TransactionID = BalanceRequestID;

                _Client.Network.SendPacket(mbrp);
                Console.WriteLine(mbrp);
                rtn = "Requesting current balance";
            }
            else
            {
                rtn = "Balance request already in progress.";
            }

            BalanceRequestMutex.ReleaseMutex();
            return rtn;
        }

        public string[] GetCommandStrings()
        {
            return new string[] { "balance" };
        }

        public string GetSummaryText()
        {
            return "Request or Monitor Money Balances.";
        }

        public string GetHelpText()
        {
            return "Usage: balance [monitor]";
        }

        #endregion
    }
}
